/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.io.soap;

/**
 * Entity that contains the SOAP request information
 * 
 * @author kentelt
 * 
 */
public class SoapMessage {

	private static String targetNamespace = "http://tempuri.org/";

	/**
	 * Set the target namespace. The default is <code>http://tempuri.org/</code>
	 * 
	 * @param namespace
	 */
	public static void setTargetNameSpace(String namespace) {
		targetNamespace = namespace;
	}

	private boolean isNullParam;
	private StringBuffer message = new StringBuffer();

	private String methodName;

	/**
	 * 
	 * Create a new Soap message
	 * 
	 * @param methodName
	 *            method to call
	 * @param paramterNames
	 *            list of parameter names. set to null if the method does not
	 *            take any parameters
	 * @param parameterValues
	 *            list of parameter values. set to null if the method does not
	 *            take any parameters
	 */
	public SoapMessage(String methodName, String[] paramterNames, String[] parameterValues) {
		this.methodName = methodName.trim();
		constructMessageBase();
		if (paramterNames != null || parameterValues != null) {
			constructMethodParams(paramterNames, parameterValues);
		} else {
			isNullParam = true;
		}
	}

	public void addParameter(String name, String value) {
		if (!isNullParam) {
			message.append('<').append(name).append('>');
			message.append(value).append("</").append(name).append(">\n");
		}
	}

	private void constructMessageBase() {
		message.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
		message
				.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n");
		message.append("<soap:Body>\n");
		message.append("<");
		message.append(methodName);
		message.append(" xmlns=\"http://tempuri.org/\"");// >\n
	}

	private void constructMethodParams(String[] name, String[] values) {
		message.append(">\n");
		if (name.length != values.length)
			throw new IllegalArgumentException("parameter name and value array length dont match");

		for (int i = 0; i < name.length; i++) {
			addParameter(name[i], values[i]);
		}
	}

	public String getRequestAction() {
		return targetNamespace + methodName;
	}

	byte[] getSoapMessage() {
		System.out.println("[SOAP MESSAGE]: " + message.toString());
		return message.toString().getBytes();
	}

	/**
	 * finish final details
	 */
	void sealMessage() {
		if (!isNullParam) {
			message.append("</").append(methodName).append(">\n");
		} else {
			message.append("/>\n");
		}
		message.append("</soap:Body>\n").append("</soap:Envelope>\n");

	}

	// public String getStrSoapMessage() {
	// return message.toString();
	// }

}
